﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Caching;

namespace YLBaseCommon.Framework
{
    public class RemoteServices<T>
    {
        ObjectCache cache = MemoryCache.Default;

        private string TokenName = "authToken";


        private static string SiteRootPath = "https://apidevelop.yla520.com";

        private string Bearer
        {
            get
            {
                
                string value =  cache[TokenName] as string;
                if (string.IsNullOrEmpty(value))
                {
                    string url = string.Format(SiteRootPath+"/{1}/{2}", ApiSite, "Oauth", "Token");
                    string json = HttpService.HttpPostData(url, "{\"UserName\":\"admin\",\"UserPwd\":\"123456\"}", "");
                    dynamic obj = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(json);
                    string accessToken = obj.access_token;
                    CacheItemPolicy policy = new CacheItemPolicy();
                    policy.AbsoluteExpiration = DateTime.Now.AddDays(6);//设置过期时间是当前时间+10秒，那么10秒后，这个缓存的项就会被移除
                    value = accessToken;//需要我们缓存的值
                    cache.Set(TokenName, value, policy);//插入缓存
                    return value;
                }
                else
                {
                    return value;
                }
            }
        }


        string ApiSite = SiteRootPath+"/api";

        public RemoteRs<T> CallJson(string area, string function, Dictionary<string, object> data, string type = "post")
        {
            try
            {
                string url = "";
                //参数的问题
                switch (type.ToLower())
                {
                    case "get":
                        url = string.Format("{0}/{1}/{2}?client=pc{3}", ApiSite, area, function, DecodeGetData(data));
                        string getRs = HttpService.HttpGetData(url, Bearer);
                        return Newtonsoft.Json.JsonConvert.DeserializeObject<RemoteRs<T>>(getRs);
                    case "post":
                        url = string.Format("{0}/{1}/{2}?client=pc", ApiSite, area, function);
                        string dataJson = Newtonsoft.Json.JsonConvert.SerializeObject(data);
                        string postRs = HttpService.HttpPostData(url, dataJson, Bearer);
                        return Newtonsoft.Json.JsonConvert.DeserializeObject<RemoteRs<T>>(postRs);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return null;
        }

        private string DecodeGetData(Dictionary<string, object> data)
        {
            StringBuilder sb = new StringBuilder();
            if (data.Count > 0)
            {
                sb.Append("&");
            }
            foreach (var item in data)
            {

                sb.Append(item.Key + "=" + item.Value + "&");
            }
            return sb.ToString().TrimEnd('&');
        }



    }
}
